package com.maker.LMMS.controller;

import com.blade.mvc.annotation.*;
import com.blade.mvc.http.Request;
import com.blade.mvc.http.Response;
import com.maker.LMMS.model.*;
import com.maker.LMMS.service.AdminImpl;
import com.maker.LMMS.service.AdminShowImpl;
import com.maker.LMMS.service.Vcode;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author : zimu
 * @Date: 2018/8/6 07:50
 * @description :管理后台
 */
@Path("/backstage")
public class AdminController {
    static int i = 0;

    @PostRoute("backstage_login")
    public void backstage_login(@Param String username, @Param String password,@Param String yzm, Request request, Response response){
        i++;
        AdminImpl user = new AdminImpl();
        if(request.session().attribute("code")==null)
            response.json("{\"tips\":\"验证码已过期！请重新获取\"}");
        else if (user.checkVcode(request, yzm)) {
            if (user.login(username, password, request)) {
                i=0;
                response.json("{\"tips\":\"1\"}");
            } else {
                response.json("{\"tips\":\"用户名或密码输入有误！\"}");
            }
        }
        else{
            response.json("{\"tips\":\"验证码输入有误！\"}");
        }
        if (i>=3){
            i=0;
            request.session().remove("code");
            System.out.println(65161616);
        }
    }

    @PostRoute("addRole")
    public void addRole(@Param String roleName, @Param String password, @Param String realName,@Param String phone,Request request, Response response){
        AdminImpl user = new AdminImpl();
        user.addRole(roleName, password,realName,phone, request);
        response.json("{\"tips\":\"添加成功！\"}");
    }

    @GetRoute("backstage_logout")
    public void backstage_logout(Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.logout(request);
        response.redirect("/backstage/signin");
    }

    @PutRoute("doUpdateInfo")
    public void doUpdateInfo(@Param int a_id,@Param String username,@Param String phone,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.updateInfo(a_id,username,phone,request);
        response.json("{\"tips\":\"修改成功！\"}");
    }

    @PostRoute("doAboutUs")
    public void doAboutUs(@Param String content,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.updateAboutus(content,request);
        response.redirect("/backstage/aboutus");
    }

    @PostRoute("doEditRole")
    public void doEditRole(@Param int roleId,@Param String realName,@Param String roleName,
                           @Param String phone,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.editRole(roleId,roleName,realName,phone,request);
        response.json("{\"tips\":\"编辑成功！\"}");
    }

    @PostRoute("doEditGroup")
    public void doEditGroup(@Param int g_id,@Param String gname,@Param int leader,
                           @Param String describe,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.editGroup(g_id,gname,leader,describe,request);
        response.json("{\"tips\":\"编辑成功！\"}");
    }

    @PostRoute("doEditUser")
    public void doEditUser(@Param(name = "member") Users member, Request request, Response response) {
        member.update("m_id",member.getM_id());
        response.json("{\"tips\":\"编辑成功！\"}");
    }

    @PostRoute("doEditGleader")
    public void doEditGleader(@Param(name = "leader") Leader leader, Request request, Response response) {
        leader.update("le_id",leader.getLe_id());
        response.json("{\"tips\":\"编辑成功！\"}");
    }

    @PostRoute("doAddGroup")
    public void doAddGroup(@Param String gname,@Param int leader,
                            @Param String describe,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.addGroup(gname,leader,describe,request);
        response.json("{\"tips\":\"添加成功！\"}");
    }

    @PostRoute("doEditHistory")
    public void doEditHistory(@Param int d_id,@Param String title,@Param String content,
                              @Param String address,@Param String time,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.editHistory(d_id,title,content,address,time,request);
        response.json("{\"tips\":\"编辑成功！\"}");
    }

    @PostRoute("doAddHistory")
    public void doAddHistory(@Param String title,@Param String content,
                              @Param String address,@Param String time,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        user.addHistory(title,content,address,time,request);
        response.json("{\"tips\":\"添加成功！\"}");
    }

    @PostRoute("doAddUser")
    public void doAddUser(@Param(name = "member") Users member,Request request, Response response) {
        member.save();
        response.json("{\"tips\":\"添加成功！(请前往注册审核查看)\"}");
    }

    @PostRoute("doAddGleader")
    public void doAddGleader(@Param(name = "leader") Leader leader,Request request, Response response) {
        leader.save();
        response.json("{\"tips\":\"添加成功！\"}");
    }

    @PutRoute("doUpatePwd")
    public void doUpatePwd(@Param int roleId,@Param String oldpwd,@Param String password,Request request, Response response) {
        AdminImpl user = new AdminImpl();
        String mes = user.updatePwd(roleId,oldpwd,password,request);
        response.json("{\"tips\":\""+mes+"\"}");
    }

    @PutRoute("doRegSetting/:flag")
    public void doRegSetting(@PathParam int flag,Request request, Response response) {
        Resigter resigter = new Resigter();
        resigter.setR_flag(flag);
        resigter.update("r_id",1);
        response.json("{\"tips\":\"success\"}");
    }

    @PostRoute("feedback_isread/:flag")
    public void feedback_unread(@PathParam int flag,@Param String ids, Request request, Response response) {
        String[] ids_ = ids.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Feedback feedback = new Feedback();
            feedback.setF_flag(flag);
            feedback.update("f_id",ids_[i]);
        }
        response.json("{\"tips\":"+i+"}");
    }

    @PostRoute("feedback_delete")
    public void feedback_delete(@Param String ids, Request request, Response response) {
        String[] ids_ = ids.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Feedback feedback = new Feedback();
            feedback.delete("f_id",ids_[i]);
        }
        response.json("{\"tips\":"+i+"}");
    }

    @PostRoute("role_delete")
    public void role_delete(@Param String ids, Request request, Response response) {
        String[] ids_ = ids.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Admin role = new Admin();
            role.delete("a_id",ids_[i]);
        }
        response.json("{\"tips\":"+i+"}");
    }

    @PostRoute("history_delete")
    public void history_delete(@Param String ids, Request request, Response response) {
        String[] ids_ = ids.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Development development = new Development();
            development.delete("d_id",ids_[i]);
        }
        response.json("{\"tips\":"+i+"}");
    }

    @PostRoute("group_delete")
    public void group_delete(@Param String ids, Request request, Response response) {
        String[] ids_ = ids.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Group group = new Group();
            group.delete("g_id",ids_[i]);
            Leader leader = new Leader();
            leader.delete("le_groupId",ids_[i]);
        }
        response.json("{\"tips\":"+i+"}");
    }

    @PostRoute("leader_delete")
    public void leader_delete(@Param String ids, Request request, Response response) {
        String[] ids_ = ids.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Leader leader = new Leader();
            leader.delete("le_id",ids_[i]);
        }
        response.json("{\"tips\":"+i+"}");
    }

    @PostRoute("user_delete")
    public void user_delete(@Param String ids, Request request, Response response) {
        String[] ids_ = ids.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Skills skills = new Skills();
            skills.delete("memberId",ids_[i]);
//            Experience experience = new Experience();
//            experience.delete("memberId",ids_[i]);
            LeaveWord leaveWord = new LeaveWord();
            leaveWord.delete("l_accepter",ids_[i]);
            leaveWord.delete("l_sender",ids_[i]);
            Users users = new Users();
            users.delete("m_id",ids_[i]);
        }
        response.json("{\"tips\":"+i+"}");
    }

    @PostRoute("doAudit/:flag")
    public void doAudit(@PathParam int flag,@Param String ids,@Param String member,
                        @Param String admin,@Param String email, Request request, Response response) {
        String[] ids_ = ids.split(",");
        String[] members = member.split(",");
        String[] emails = email.split(",");
        int i;
        for (i=0;i<ids_.length;i++)
        {
            Users user = new Users();
            Checklog checklog = new Checklog();
            if (flag==1) {
                user.setM_flag(flag);
                user.update("m_id", ids_[i]);
            }
            else if (flag==-1){
                user.delete("m_id",ids_[i]);
            }
            //获取任务发布的时间
            Date date =new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd  HH:mm:ss");
            String date1 = sdf.format(date);

            checklog.setC_member(members[i]);
            checklog.setC_admin(admin);
            checklog.setC_email(emails[i]);
            checklog.setC_time(date1);
            checklog.setC_result(flag);
            checklog.save();
        }
        response.json("{\"tips\":"+i+"}");
    }

    //===================管理后台·路由=======================
    @GetRoute("/")
    public String backstage(Request request){
        AdminImpl show = new AdminImpl();
//        show.showIndex(request);
//        show.showSomeNewMember(request);
//        show.showHeadMan(request);
        return "backstage/index";
    }

    @GetRoute("/audit")
    public String audit( Request request){
        String  key = String.valueOf(request.query("con", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1)
            page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showAudit(request,key,page);
        return "backstage/audit";
    }

    @GetRoute("/audit2")
    public String audit2( Request request){
        String  key = String.valueOf(request.query("con", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1)
            page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showAudit2(request,key,page);
        return "backstage/audit-2";
    }

    @GetRoute("/regSetting")
    public String regSetting(Request request){
        AdminShowImpl show = new AdminShowImpl();
        show.showRegSetting(request);
        return "backstage/regSetting";
    }

    @GetRoute("/signin")
    public String backstage_sigin(Request request, @Param String uri, Response response) {
        i=0;
        Vcode vcode = new Vcode();
        vcode.doGet(request, response);
        request.attribute("uri", uri);
        System.out.println(uri+666);
        return "backstage/signin";
    }

    @GetRoute("/feedback")
    public String feedback(Request request){
        String  key = String.valueOf(request.query("key", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1)
            page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showFeedback(request,0,key,page);
        return "backstage/feedback";
    }

    @GetRoute("/feedback2")
    public String feedback2(Request request){
        String  key = String.valueOf(request.query("key", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1) page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showFeedback(request,1,key,page);
        return "backstage/feedback-2";
    }

    @GetRoute("/feedbackDetail/:id")
    public String feedbackDetail(Request request,@PathParam int id){
        AdminShowImpl show = new AdminShowImpl();
        show.feedbackDetail(id,request);
        return "backstage/feedback_Detail";
    }

    @GetRoute("/group")
    public String group(Request request){
        String  key = String.valueOf(request.query("key", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1) page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showGroup(request,key,page);
        return "backstage/group";
    }

    @GetRoute("/gleader/:id")
    public String gleader(Request request,@PathParam int id){
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1) page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showGleader(request,page,id);
        return "backstage/gleader";
    }

    @GetRoute("/addGroup")
    public String addGroup(){
        return "backstage/addGroup";
    }

    @GetRoute("/editGroup/:id")
    public String editGroup(Request request,@PathParam int id){
        AdminShowImpl show = new AdminShowImpl();
        show.showEditGroup(request,id);
        return "backstage/editGroup";
    }

    @GetRoute("/addGleader/:id")
    public String addGleader(Request request,@PathParam int id){
        request.attribute("groupid",id);
        return "backstage/addGleader";
    }

    @GetRoute("/editGleader/:id")
    public String editGleader(Request request,@PathParam int id){
        AdminShowImpl show = new AdminShowImpl();
        show.showEditGleader(request,id);
        return "backstage/editGleader";
    }

    @GetRoute("/role")
    public String role(Request request){
        String  key = String.valueOf(request.query("key", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1) page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showAdmin(request,key,page);
        return "backstage/role";
    }

    @GetRoute("/editRole/:id")
    public String editRole(Request request,@PathParam int id){
        AdminShowImpl show = new AdminShowImpl();
        show.showEditRole(request,id);
        return "backstage/editRole";
    }

    @GetRoute("/addRole")
    public String addRole(){
        return "backstage/addRole";
    }

    @GetRoute("/shareRole")
    public String shareRole(){
        return "backstage/shareRole";
    }

    @GetRoute("/updatePwd")
    public String updatePwd(){
        return "backstage/updatePwd";
    }

    @GetRoute("/updateInfo")
    public String updateInfo(){
        return "backstage/updateInfo";
    }

    @GetRoute("/user")
    public String user(Request request){
        String  key = String.valueOf(request.query("key", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        String orderby = request.query("orderby", "m_group");
        if(page<1) page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showMember(request,key,page,orderby);
        return "backstage/user";
    }

    @GetRoute("/editUser/:id")
    public String editUser(Request request,@PathParam int id){
        AdminShowImpl show = new AdminShowImpl();
        show.showEditUser(request,id);
        return "backstage/editUser";
    }

    @GetRoute("/lookUser/:id")
    public String lookUser(Request request,@PathParam int id){
        AdminShowImpl show = new AdminShowImpl();
        show.showEditUser(request,id);
        return "backstage/lookUser";
    }

    @GetRoute("/addUser")
    public String addUser(Request request){
        AdminShowImpl show = new AdminShowImpl();
        show.allGroup(request);
        return "backstage/addUser";
    }

    @GetRoute("/substance")
    public String substance(Request request){
        String  key = String.valueOf(request.query("key", ""));
        int page = Integer.parseInt(request.query("page", "1"));
        if(page<1) page=1;
        AdminShowImpl show = new AdminShowImpl();
        show.showDevelopment(request,key,page);
        return "backstage/substance";
    }

    @GetRoute("/aboutus")
    public String backstage_aboutus(Request request){
        AdminShowImpl show = new AdminShowImpl();
        show.showAboutus(request);
        return "backstage/aboutus";
    }

    @GetRoute("/editHistory/:id")
    public String editHistory(Request request,@PathParam int id){
        AdminShowImpl show = new AdminShowImpl();
        show.showEditHistory(request,id);
        return "backstage/editHistory";
    }

    @GetRoute("/addHistory")
    public String addHistory(){
        return "backstage/addHistory";
    }

    @GetRoute("/404")
    public String error(){
        return "backstage/404";
    }





}
